package com.apexsoft.monitor.front.console.scheme.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apexsoft.front.common.support.BaseFrontController;
import com.apexsoft.front.common.support.Result;
import com.apexsoft.front.common.utils.annotation.CrudMethod;
import com.apexsoft.monitor.front.console.aop.AopAnnotation;
import com.apexsoft.monitor.front.console.scheme.service.SchemeService;
import com.apexsoft.monitor.front.console.pageModel.SchemePageModel;
import com.apexsoft.monitor.front.console.task.service.TaskService;
import com.baomidou.mybatisplus.plugins.pagination.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* 监控方案
*/
@Controller("schemeController")
@RequestMapping("/console/scheme")
public class SchemeController extends BaseFrontController {
    @Autowired
    @Qualifier("schemeService")
    private SchemeService schemeService = null;

    @Autowired
    @Qualifier("taskService")
    private TaskService taskService = null;

    /**
    * 分页查询
    * @param request
    * @param response
    * @param modelMap
    * @return
    * @throws Exception
    */
    @RequestMapping(value = "/listByPage")
    public String listByPage(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws Exception {
    return "console/scheme/schemeList";
    }

    @RequestMapping(value = "/loadSchemeByPage")
    public String getschemeList(HttpServletRequest request, HttpServletResponse response,@ModelAttribute("form") SchemePageModel schemePageModel)throws Exception {
        Pagination page =new Pagination(schemePageModel.getPageNum(),schemePageModel.getPageSize());
        JSONObject result = this.schemeService.listByPage(page,schemePageModel.getSchemeName());
        request.setAttribute("result",result);
        this.printJSON(response,result);
        return null;
    }

    @CrudMethod
    @AopAnnotation(description="添加监控方案")
    @RequestMapping(value = "/add")
    public String add(HttpServletRequest request, HttpServletResponse response) throws Exception {
        JSONObject result = schemeService.add(request);
        this.printJSON(response,result);
        return null;
    }
    @CrudMethod
    @AopAnnotation(description="修改监控方案")
    @RequestMapping(value = "/update")
    public String update(HttpServletRequest request, HttpServletResponse response) throws Exception {
        JSONObject result = schemeService.update(request);
        this.printJSON(response,result);
        return null;
    }
    @CrudMethod
    @AopAnnotation(description="删除监控方案")
    @RequestMapping(value = "/delete")
    public String delete(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String id = request.getParameter("id");
        JSONObject result = schemeService.delete(id);
        this.printJSON(response,result);
        return null;
    }

    /**
     * 跳转至监控图编辑页面
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/goSchemeDraw")
    public String goSchemeDraw(HttpServletRequest request,HttpServletResponse response) throws Exception{
        String schemeCode = request.getParameter("schemeCode");
        String schemeTitle = request.getParameter("schemeTitle");
        request.setAttribute("schemeCode",schemeCode);
        request.setAttribute("schemeTitle",schemeTitle);
        return "console/scheme/schemeDraw";
    }

    /**
     * 跳转至监控方案显示页面
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/goSchemeShow")
    public String goSchemeShow(HttpServletRequest request,HttpServletResponse response) throws Exception{
        return "console/scheme/schemeShow";
    }
    /**
     * 更新拓扑图内容
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping(value = "/updateContent")
    public void updateContent(HttpServletRequest request,HttpServletResponse response) throws Exception{
        JSONObject result = schemeService.updateContent(request);
        this.printJSON(response,result);
    }

    /**
     * 获取拓扑图内容
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping(value = "/getContent")
    public void getContent(HttpServletRequest request,HttpServletResponse response) throws Exception{
        JSONObject result = schemeService.getContent(request);
        this.printJSON(response,result);
    }

    /**
     * 查询监控方案列表
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping(value = "/getAllScheme")
    public void getAllScheme(HttpServletRequest request,HttpServletResponse response) throws Exception{
        JSONArray array = new JSONArray();
        JSONObject result = schemeService.getAllScheme();
        if (result != null && result.getIntValue(Result.CODE) > 0) {
            array.addAll(result.getJSONArray(Result.ROWS));
        }
        this.printJSON(response, array);
    }

    /**
     * 查询
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping(value = "/queryNodeDesc")
    public void queryNodeDesc(HttpServletRequest request,HttpServletResponse response) throws Exception{
        String nodeCode = request.getParameter("nodeCode");
        JSONObject result = schemeService.queryNodeDesc(nodeCode);
        this.printJSON(response,result);
    }

    /**
     * 查询中件间节点对应的任务列表
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping(value = "/queryTaskList")
    public void queryTaskList (HttpServletRequest request,HttpServletResponse response) throws Exception{
        String nodeCode = request.getParameter("nodeCode");
        JSONObject result = taskService.queryTaskByNodeCode(nodeCode);
        this.printJSON(response,result);
    }

    /**
     * 获取监控拓扑图信息
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping(value = "/getSchemeByCode")
    public void getSchemeByCode (HttpServletRequest request,HttpServletResponse response) throws Exception{
        String schemeCode = request.getParameter("schemeCode");
        JSONObject result = schemeService.getSchemeByCode(schemeCode);
        this.printJSON(response,result);
    }
}

